package hcontroller

import (
	"encoding/json"

	"advertoper/controllers"
	"advertoper/errorcode"
	"advertoper/homepageinfo/hservice"
)

type ZJGoodsListInfoController struct {
	controllers.SCLoginedController
}

//上报商品销售+1
type GoodsSellNumUp struct {
	controllers.URLSignController
}

/*添加zjGoodsList
{"region_code":"","clickable":1,"title":"油烟机清洗79元","sub_title":"给你的油烟机洗洗澡",
"pic_url":"https://fscdn.qkier.com/scloud/e/b/c/d725bad2001dd6c76a189dd6e0f69",
"background_picture":"https://fscdn.qkier.com/scloud/e/b/c/d725bad2001dd6c76a189dd6e0f69",
"background_color":"#000000","starttick":1,"endtick":1499999999,"price":7990,"sell_number":11,
"routers":"zhjt://app.zjdj.com/webin?http://www.zhijiandaojia.com"}
*/
// @router /zjgoodslist/add [post]
func (p *ZJGoodsListInfoController) ZJGoodsListAdd() {
	defer p.Output("ZJGoodsListAdd")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	req := &hservice.ZJGoodsListAddReq{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := hservice.GetZJGoodsListInfoService().ZJGoodsListAdd(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg

}

/*修改zjGoodsList
{"id":1,"region_code":"","clickable":1,"title":"油烟机清洗79元","sub_title":"给你的油烟机洗洗澡",
"pic_url":"https://fscdn.qkier.com/scloud/e/b/c/d725bad2001dd6c76a189dd6e0f69",
"background_picture":"https://fscdn.qkier.com/scloud/e/b/c/d725bad2001dd6c76a189dd6e0f69",
"background_color":"#000000","starttick":1,"endtick":1499999999,"price":7990,"sell_number":11,
"routers":"zhjt://app.zjdj.com/webin?http://www.zhijiandaojia.com","status":1}
*/
// @router /zjgoodslist/update [post]
func (p *ZJGoodsListInfoController) ZJGoodsListUpdate() {
	defer p.Output("ZJGoodsListUpdate")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	req := &hservice.ZJGoodsListUpdateReq{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := hservice.GetZJGoodsListInfoService().ZJGoodsListUpdate(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg

}

//web查询
// @router /zjgoodslist/list [get]
func (p *ZJGoodsListInfoController) ZJGoodsListInfoList() {
	defer p.Output("ZJGoodsListInfoList")
	p.ForceHTTPS()

	status, _ := p.GetInt("status", 1)
	data := hservice.GetZJGoodsListInfoService().ZJGoodsListInfoList(status)
	if len(data) == 0 {
		p.Resp["code"] = 101
		p.Resp["msg"] = "no data"
	} else {
		p.Resp["code"] = 0
		p.Resp["msg"] = "success"
		p.Resp["data"] = data
	}

}

//商品销售数量增加上报
// @router /zjgoodslist/sellnum/up [post]
func (p *GoodsSellNumUp) GoodsSellNumUp() {
	defer p.Output("GoodsSellNumUp")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	data := make(map[string]int)
	if err := json.Unmarshal(body, &data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	goodsId := data["goods_id"]

	code, msg := hservice.GetZJGoodsListInfoService().GoodsSellNumUp(goodsId)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}
